package com.xur.springboot.controller;

import com.xur.springboot.entity.Tabclass;
import com.xur.springboot.entity.User;
import com.xur.springboot.service.TabService;
import com.xur.springboot.service.UserService;
import com.xur.springboot.utils.ResultVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/doRegister")
    public String doRegister(User user) {
        int result = userService.addUserInfo(user);
        System.out.println("注册中————————————");
        return "true";
    }


    @RequestMapping("/login")
    public ResultVO login(String username, String password, int role){


        System.out.println("------"+username+"登录系统"+"------");
        System.out.println("------"+"他的密码是："+password+"------");
        System.out.println("------"+"他的role角色是："+role+"------");
        //调用业务层
        /*boolean login = userService.login(username,password);
        if (login){
            return "login";
        }else {
            return "success";
        }*/
        return userService.login(username,password,role);
    }
    @GetMapping("/queryIdByUsername")
    public int queryIdByUsername(@RequestParam String username,@RequestParam String password){
        return userService.queryIdByUsername(username,password);

    }



    //在鸿蒙前端获取数据时需要解析JSON数据
    //直接返回json数据
    @GetMapping("/userList")
    public List<User> Users(){
        return userService.queryUser();

    }


    // 新增状态修改接口
    @GetMapping("/status/{userId}")
    public ResultVO updateUserStatus(@PathVariable Integer userId, @RequestParam Integer status) {
        try {
            int result = userService.updateUserStatus(userId, status);
            return result > 0 ?
                    new ResultVO(1001, "状态更新成功", null) :
                    new ResultVO(1000, "用户不存在", null);
        } catch (Exception e) {
            return new ResultVO(5000, "服务器错误: " + e.getMessage(), null);
        }
    }
    @GetMapping("/queryUserById")
    public List<User> queryUserById(@RequestParam int id){
        return userService.queryUserById(id);

    }

    @GetMapping("/queryUsersByDynamicFields")
    public ResultVO queryUsersByDynamicFields(
            @RequestParam("fields") List<String> fields, // 字段名列表，如 ["id", "id1"]
            @RequestParam("targetId") Integer targetId    // 目标ID值
    ) {
        try {
            List<User> users = userService.queryUsersByDynamicFields(fields, targetId);
            return new ResultVO(1001, "查询成功", users);
        } catch (Exception e) {
            return new ResultVO(5000, "服务器错误: " + e.getMessage(), null);
        }
    }


    @GetMapping("/queryUsersByIds")
    public List<User> queryUsersByIds(@RequestParam("ids") String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        return userService.queryUsersByIds(idList);
    }
}
